package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Teacher;

import java.util.ArrayList;

public class OtherTeacherDao implements BaseTeacherDao {
    //创建容器
    private ArrayList<Teacher> teachers= new ArrayList<>();


    @Override
    public boolean addTeacher(Teacher teacher) {
        return teachers.add(teacher);
    }

    @Override
    public Teacher[] findAllTeachers() {
        int length= teachers.size();
        Teacher[] result=new Teacher[length];
        for (int i = 0; i < teachers.size(); i++) {
            Teacher teacher = teachers.get(i);
            result[i]= teacher;
        }
        return result;
    }

    @Override
    public void deleteTeacher(String deleteId) {
        int index = getIndex(deleteId);
        teachers.remove(index);

    }

    @Override
    public int getIndex(String deleteId) {
        int index=-1;
        for (int i = 0; i < teachers.size(); i++) {
            //teachers.get(i);
            Teacher teacher = teachers.get(i);
            if(teacher!=null && teacher.getSid().equals(deleteId)){
                index=i;
                break;
            }
        }

        return index;
    }

    @Override
    public void updateTeacher(String updateId, Teacher teacher) {
        int index = getIndex(updateId);
        teachers.set(index,teacher);

    }

    @Override
    public void deleteTeacherByIndex(int index) {
        teachers.remove(index);
    }
}
